/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import com.sun.faces.component.visit.FullVisitContext;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import model.BrowserModel;

import org.primefaces.component.selectonelistbox.SelectOneListbox;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.component.selectoneradio.SelectOneRadio;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Ihab Ali (ali00173@algonquinLive.com)
 */
@ManagedBean(name="bean")
@SessionScoped

public class BrowserController implements Serializable {
    private static final long serialVersionUID = -780867148371960098L;
    private BrowserModel browserModel ;
    private boolean validInput = true;
   
    
    public BrowserController() throws ClassNotFoundException {
       super();

       browserModel = new BrowserModel();
    }
  
  public BrowserModel getBrowserModel() {
        return browserModel;
    }  
 

  public void handlePublic(){
      
      System.out.println( browserModel.isPublicButton());
      browserModel.populateMembers();
  }
  

 public void pageSubmitted() throws ClassNotFoundException{
     UIComponent search =  findComponent("textBar");
        if (search != null) {
            System.out.println("******* we found it  **********");
            String searchText = (String) ((SelectOneMenu) search).getValue();
            System.out.println("******* " + searchText + "  **********");
            if (browserModel.isInSearchHistory(searchText)) {
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, new FacesMessage("Class " + searchText + 
                        " already Displayed \n use class navagiation or try new search"));
                return;
            }
        }
        if (!browserModel.isClassNotFoundException()) {
            browserModel.setPackageToSearch();
            browserModel.createClassHeiraracy();
            browserModel.populateMembers();
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Class " + browserModel.getErrorClass() + " not found"));
        }
    }


 public void valueChanged (ValueChangeEvent abe){
     System.out.println("******* I was fired from Listener**********");
    //this.browserModel.setSearchItem((String) ((SelectOneMenu)abe.getComponent()).getValue());
 }
 
 public void dontChangeIt(ValueChangeEvent vce){
     System.out.println("******* package listener**********");
     browserModel.setPackageToSearch();
     RequestContext.getCurrentInstance().update(":searchBar:layoutPKG");
 }
 
 public void dontChangeIt(){
     System.out.println("******* package Ajax listener**********");
     browserModel.setPackageToSearch();
     RequestContext.getCurrentInstance().update("searchBar");
 }
 
 public void radioButtonClicked (){
      UIComponent search =  findComponent("customRadio");
      if(search != null){
          String searchText = (String) ((SelectOneRadio)search).getValue();
          System.out.println("New Radio button Value "+searchText);
          browserModel.setRadioOption(searchText);
      }
 }
 
 public void memberSelected(){
      UIComponent search =  findComponent("memebersTodisplay");
      System.out.println("New memeber Selected  ");
      if(search != null){
          System.out.println("found it "+((SelectOneListbox)search).getValue().toString());
         
          browserModel.setDeclaration( ((SelectOneListbox)search).getValue().toString());
//          if(((SelectOneListbox)search).getValue() instanceof Method){
//              System.out.println("New memeber Selected  "+ ((Method)((SelectOneListbox)search).getValue()).getName());
//              browserModel.methodSelected( (Method)((SelectOneListbox)search).getValue() );
//          }else   if(((SelectOneListbox)search).getValue() instanceof Constructor){
//              System.out.println("New memeber Selected  "+ ((Constructor)((SelectOneListbox)search).getValue()).getName());
//              browserModel.constructorSelected((Constructor)((SelectOneListbox)search).getValue());
//          }else   if(((SelectOneListbox)search).getValue() instanceof Field){
//              System.out.println("New memeber Selected  "+ ((Field)((SelectOneListbox)search).getValue()).getName());
//              browserModel.fieldSelected((Field)((SelectOneListbox)search).getValue());
//          }
      }
 }
 
 public UIComponent findComponent(final String id){
    FacesContext context = FacesContext.getCurrentInstance(); 
    UIViewRoot root;
        root = context.getViewRoot();
    final UIComponent[] found = new UIComponent[1];
    root.visitTree(new FullVisitContext(context), new VisitCallback() {     
        @Override
        public VisitResult visit(VisitContext context, UIComponent component) {
            if(component.getId().equals(id)){
                found[0] = component;
                return VisitResult.COMPLETE;
            }
            return VisitResult.ACCEPT;              
        }
    });
    return found[0];
}
 }
    
 
    
